package com.jeesite.modules.config;


import com.jeesite.common.beetl.BeetlUtils;
import com.jeesite.common.collect.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.GroupTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * xcf 增加上下文变量
 * 实现InitializingBean类，初始化后给Beetl设置共享变量
 */
@Slf4j
@Service
public class BeetlInitializingBean implements InitializingBean {
    private GroupTemplate groupTemplate;

    @Value("${pic.baseUrl}")
    private String baseUrl;

    @Value("${pic.host}")
    private String host;

    @Override
    public void afterPropertiesSet()  {
        // 设置Beetl GroupTemplate，如果不设置，取上下文中唯一的GroupTemplate对象
        this.groupTemplate = BeetlUtils.getResourceGroupTemplate();

        // 设置Beetl全局变量
        Map<String, Object> sharedVars = this.groupTemplate.getSharedVars();
        if (sharedVars == null){
            sharedVars = MapUtils.newHashMap();
        }
        sharedVars.put("qianduanBaseUrl", host);
        sharedVars.put("houduanBaseUrl", baseUrl);
        // sharedVars.put("你的全局变量名2，如ctxStatic2", '你的全局变量值如ctxPath/static2');
        this.groupTemplate.setSharedVars(sharedVars);
    }
}


